class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        string s = "croak";
        int n = s.size();
        vector<int> hash(n);

        unordered_map<char, int> index;
        for (int i = 0; i < n; i++)
            index[s[i]] = i;

        for (auto str : croakOfFrogs)
        {
            if (str == 'c')
            {
                if (hash[n - 1] != 0)
                    hash[n - 1]--;
                hash[0]++;
            }
            else
            {
                int i = index[str];
                if (hash[i - 1] != 0)
                    hash[i - 1]--;
                else
                    return -1;
                hash[i]++;
            }
        }
        for (int i = 0; i < n - 1; i++)
            if (hash[i] != 0)
                return -1;

        return hash[n - 1];
    }
};